package com.betterjr.modules.customer.service;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONObject;
import com.betterjr.common.utils.BTAssert;
import com.betterjr.common.utils.BetterDateUtils;
import com.betterjr.common.utils.Collections3;
import com.betterjr.common.utils.QueryTermBuilder;
import com.betterjr.common.utils.UserUtils;
import com.betterjr.modules.account.service.CustAccountService;
import com.betterjr.modules.customer.AbstractBaseMechCustTemDataHandlerAdapter;
import com.betterjr.modules.customer.dao.CustMechContacterMapper;
import com.betterjr.modules.customer.entity.CustMechContacter;
import com.betterjr.modules.customer.entity.CustMechContacterTmp;
import com.betterjr.modules.document.service.CustFileItemService;
import com.betterjr.modules.temhandler.data.TemDataConstant;
import com.betterjr.modules.temhandler.entity.TemData;
import com.betterjr.modules.temhandler.entity.TemDataBuilder;
import com.betterjr.modules.temhandler.service.TemDataService;

/**
 * 
 * @author liuwlBaseService<CustMechContacterMapper, CustMechContacter>
 *
 */
@Service
public class CustMechContacterService
        extends AbstractBaseMechCustTemDataHandlerAdapter<CustMechContacterMapper, CustMechContacter> {

    @Resource
    private CustMechContacterTmpService contacterTmpService;

    @Resource
    private CustFileItemService fileItemService;

    @Resource
    private CustAccountService accountService;

    @Autowired
    private TemDataService temDataService;

    /**
     * 查询联系人列表
     * @param anCustNo
     * @return
     */
    public List<CustMechContacter> queryCustMechContacter(Long anCustNo) {
        BTAssert.notNull(anCustNo, "客户编号不允许为空！");

        return this.selectByProperty("custNo", anCustNo);
    }

    /**
     * 查询联系人信息
     */
    public CustMechContacter findContacter(Long anId) {
        BTAssert.notNull(anId, "联系人编号不允许为空！");

        CustMechContacter contacter = this.selectByPrimaryKey(anId);
        return contacter;
    }

    /**
     * 添加联系人信息
     */
    public CustMechContacter addCustMechContacter(CustMechContacterTmp anContacterTmp) {
        BTAssert.notNull(anContacterTmp, "联系人流水信息不允许为空！");

        CustMechContacter contacter = new CustMechContacter();
        contacter.initAddValue(anContacterTmp);

        this.insert(contacter);
        return contacter;
    }

    /**
     * 保存联系人信息
     */
    public CustMechContacter saveCustMechContacter(CustMechContacter anContacter, Long anId) {
        BTAssert.notNull(anId, "联系人编号不允许为空！");
        BTAssert.notNull(anContacter, "联系人信息不允许为空！");

        final CustMechContacter tempCustMechContacter = this.selectByPrimaryKey(anId);
        BTAssert.notNull(tempCustMechContacter, "对应的联系人信息没有找到！");

        tempCustMechContacter.initModifyValue(anContacter);
        this.updateByPrimaryKeySelective(tempCustMechContacter);
        return tempCustMechContacter;
    }

    /**
     * 通过联系人流水修改联系人 
     */
    public CustMechContacter saveCustMechContacter(CustMechContacterTmp anContacterTmp) {
        BTAssert.notNull(anContacterTmp, "联系人流水编号不允许为空！");

        final CustMechContacter tempCustMechContacter = this.selectByPrimaryKey(anContacterTmp.getRefId());
        BTAssert.notNull(tempCustMechContacter, "对应的联系人信息没有找到！");

        tempCustMechContacter.initModifyValue(anContacterTmp);
        this.updateByPrimaryKeySelective(tempCustMechContacter);
        return tempCustMechContacter;
    }

    @SuppressWarnings("unchecked")
    @Override
    public TemData saveAddOrUpdateTemData(TemData anTemData) {
        // 插入校验
        Map<String, Object> map = QueryTermBuilder.newInstance().put("custNo", anTemData.getCustNo()).build();
        List<TemData> temDataList = temDataService.queryTemData(anTemData.getBusinTableType(), "1,2,3", map, "2", 1,
                10);
        BTAssert.isTrue(Collections3.isEmpty(temDataList), "已经有变更在申请过程中");
        return temDataService.saveAddTemData(anTemData);
    }

    public CustMechContacter saveAddCustMechContacterTmp(CustMechContacter custMechContacter, String anFileList) {

        BTAssert.notNull(custMechContacter, "公司基本信息-流水信息 不能为空！");
        BTAssert.notNull(custMechContacter.getCustNo(), "公司基本信息-流水信息 不能为空！");
        custMechContacter.saveAddInitValue(UserUtils.getOperatorInfo());
        custMechContacter
                .setBatchNo(fileItemService.updateCustFileItemInfo(anFileList, custMechContacter.getBatchNo()));
        TemData anTemData = convertToTemData(custMechContacter);
        saveAddOrUpdateTemData(anTemData);
        return custMechContacter;

    }

    private TemData convertToTemData(CustMechContacter custMechContacter) {

        TemData anTemData = TemDataBuilder.newInstance().businTableData(JSONObject.toJSONString(custMechContacter))
                .businTableId(custMechContacter.getId() + "").businTableOperorg(custMechContacter.getOperOrg())
                .businTableType(custMechContacter.getClass().getName()).laster(TemDataConstant.TEMDATA_LASTER_YES)
                .custNo(custMechContacter.getCustNo())
                .custName(accountService.selectByPrimaryKey(custMechContacter.getCustNo()).getCustName())
                .regDate(BetterDateUtils.getNumDate()).regOperid(UserUtils.getOperatorInfo().getId())
                .regOpername(UserUtils.getOperatorInfo().getName()).regTime(BetterDateUtils.getNumTime())
                .businStatus(TemDataConstant.TEMDATA_BUSINSTATUS_TEMTABLEINSERT).build();

        if (custMechContacter.getRefId() != null) {
            anTemData.setPrefixId(custMechContacter.getRefId());
        }
        return anTemData;
    }

}